package com.eqdd.yiqidian.ui.share;

import android.net.Uri;

import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.rongyun.message.link.LinkMessage;
import com.eqdd.yiqidian.rongyun.message.mingpian.MingpianMessage;
import com.eqdd.yiqidian.utils.ToastUtil;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;
import io.rong.message.ImageMessage;
import io.rong.message.LocationMessage;
import io.rong.message.MediaMessageContent;
import io.rong.message.RichContentMessage;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by lvzhihao on 17-5-13.
 */

public class ShareUtil {

    public static final int TEXT = 1;
    public static final int IMAGE = 2;
    public static final int MULTI_IMAGE = 3;

    public static final int FROM_EQD = 1;
    public static final int FROM_OTHER = 2;
    public static final int FROM_EQD_GET = 3;

    public static void sendMyself(BaseActivity baseActivity, List<SelectBean> shareBeanList) {
        if (shareBeanList != null && shareBeanList.size() > 0) {
            baseActivity.showLoading();
            Observable.from(shareBeanList)
                    .subscribe(new Subscriber<SelectBean>() {
                        @Override
                        public void onCompleted() {
                            baseActivity.hideLoading();
                            ToastUtil.showShort("发送成功");
                            baseActivity.finish();
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(SelectBean shareBean) {
                            if (shareBean.getType() == Conversation.ConversationType.PRIVATE) {
                                RongIM.getInstance().refreshUserInfoCache(new UserInfo(shareBean.getGuid(), shareBean.getContent(), Uri.parse(shareBean.getHeadImg())));
                            }
                            if (AppConstent.messageContent != null && (AppConstent.messageContent instanceof TextMessage ||
                                    AppConstent.messageContent instanceof RichContentMessage ||
                                    AppConstent.messageContent instanceof VoiceMessage)) {
                                Message myMessage = Message.obtain(shareBean.getGuid(), shareBean.getType(), AppConstent.messageContent);
                                RongIM.getInstance().sendMessage(myMessage, null, null, new IRongCallback.ISendMessageCallback() {
                                    @Override
                                    public void onAttached(Message message) {
                                        //消息本地数据库存储成功的回调
                                    }

                                    @Override
                                    public void onSuccess(Message message) {
                                        //消息通过网络发送成功的回调
                                        AppConstent.messageContent = null;
                                    }

                                    @Override
                                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                        //消息发送失败的回调
                                        AppConstent.messageContent = null;
                                    }
                                });
                            } else if (AppConstent.messageContent != null && (AppConstent.messageContent instanceof LocationMessage)) {
                                Message myMessage = Message.obtain(shareBean.getGuid(), shareBean.getType(), AppConstent.messageContent);
                                RongIM.getInstance().sendLocationMessage(myMessage, null, null, new IRongCallback.ISendMessageCallback() {
                                    @Override
                                    public void onAttached(Message message) {
                                        //消息本地数据库存储成功的回调
                                    }

                                    @Override
                                    public void onSuccess(Message message) {
                                        //消息通过网络发送成功的回调
                                        AppConstent.messageContent = null;
                                    }

                                    @Override
                                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                        //消息发送失败的回调
                                        AppConstent.messageContent = null;
                                    }
                                });
                            } else if (AppConstent.messageContent != null && (AppConstent.messageContent instanceof ImageMessage)) {
                                RongIM.getInstance().sendImageMessage(
                                        shareBean.getType(),
                                        shareBean.getGuid(), AppConstent.messageContent,
                                        null, null, new RongIMClient.SendImageMessageCallback() {

                                            @Override
                                            public void onAttached(Message message) {
                                                //保存数据库成功
                                            }

                                            @Override
                                            public void onError(Message message, RongIMClient.ErrorCode code) {
                                                //发送失败
                                                AppConstent.messageContent = null;
                                            }

                                            @Override
                                            public void onSuccess(Message message) {
                                                //发送成功
                                                AppConstent.messageContent = null;
                                            }

                                            @Override
                                            public void onProgress(Message message, int progress) {
                                                //发送进度
                                            }
                                        });
                            } else if (AppConstent.messageContent != null && (AppConstent.messageContent instanceof MediaMessageContent)) {

                                Message myMessage = Message.obtain(shareBean.getGuid(), shareBean.getType(), AppConstent.messageContent);
                                RongIM.getInstance().sendMediaMessage(myMessage, null, null, new IRongCallback.ISendMediaMessageCallback() {
                                    @Override
                                    public void onProgress(Message message, int i) {

                                    }

                                    @Override
                                    public void onCanceled(Message message) {
                                        AppConstent.messageContent = null;
                                    }

                                    @Override
                                    public void onAttached(Message message) {

                                    }

                                    @Override
                                    public void onSuccess(Message message) {
                                        AppConstent.messageContent = null;
                                    }

                                    @Override
                                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                        AppConstent.messageContent = null;
                                    }
                                });
                            } else if (AppConstent.messageContent != null && (AppConstent.messageContent instanceof MingpianMessage)) {

                                Message myMessage = Message.obtain(shareBean.getGuid(), shareBean.getType(), AppConstent.messageContent);
                                RongIM.getInstance().sendMessage(myMessage, null, null, new IRongCallback.ISendMessageCallback() {

                                    @Override
                                    public void onAttached(Message message) {

                                    }

                                    @Override
                                    public void onSuccess(Message message) {
                                        AppConstent.messageContent = null;
                                    }

                                    @Override
                                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                        AppConstent.messageContent = null;
                                    }
                                });
                            } else if (AppConstent.messageContent != null && (AppConstent.messageContent instanceof LinkMessage)) {

                                Message myMessage = Message.obtain(shareBean.getGuid(), shareBean.getType(), AppConstent.messageContent);
                                RongIM.getInstance().sendMessage(myMessage, null, null, new IRongCallback.ISendMessageCallback() {

                                    @Override
                                    public void onAttached(Message message) {

                                    }

                                    @Override
                                    public void onSuccess(Message message) {
                                        AppConstent.messageContent = null;
                                    }

                                    @Override
                                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                        AppConstent.messageContent = null;
                                    }
                                });
                            } else {
                                ToastUtil.showShort("不支持此类型");
                            }
                        }
                    });
        }

    }


    public static void sendText(BaseActivity baseActivity, List<SelectBean> shareBeanList, String text) {
        if (shareBeanList != null && shareBeanList.size() > 0) {
            baseActivity.showLoading();
            Observable.from(shareBeanList)
                    .subscribe(new Subscriber<SelectBean>() {
                        @Override
                        public void onCompleted() {
                            baseActivity.hideLoading();
                            ToastUtil.showShort("发送成功");
//                            baseActivity.setResult(ShareEnterActivity.FINISH);
                            baseActivity.finish();
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(SelectBean shareBean) {
                            if (shareBean.getType() == Conversation.ConversationType.PRIVATE) {
                                RongIM.getInstance().refreshUserInfoCache(new UserInfo(shareBean.getGuid(), shareBean.getContent(), Uri.parse(shareBean.getHeadImg())));
                            }
                            TextMessage myTextMessage = TextMessage.obtain(text);
                            Message myMessage = Message.obtain(shareBean.getGuid(), shareBean.getType(), myTextMessage);
                            RongIM.getInstance().sendMessage(myMessage, null, null, new IRongCallback.ISendMessageCallback() {
                                @Override
                                public void onAttached(Message message) {
                                    //消息本地数据库存储成功的回调
                                }

                                @Override
                                public void onSuccess(Message message) {
                                    //消息通过网络发送成功的回调
                                }

                                @Override
                                public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                    //消息发送失败的回调
                                }
                            });
                        }
                    });
        }
    }

    public static void sendImg(BaseActivity baseActivity, List<SelectBean> shareBeanList, Uri uri) {
        if (shareBeanList != null && shareBeanList.size() > 0) {
            baseActivity.showLoading();
            Observable.from(shareBeanList)
                    .subscribe(new Subscriber<SelectBean>() {
                        @Override
                        public void onCompleted() {
                            baseActivity.hideLoading();
                            ToastUtil.showShort("发送成功");
//                            baseActivity.setResult(ShareEnterActivity.FINISH);
                            baseActivity.finish();
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(SelectBean shareBean) {
                            if (shareBean.getType() == Conversation.ConversationType.PRIVATE) {
                                RongIM.getInstance().refreshUserInfoCache(new UserInfo(shareBean.getGuid(), shareBean.getContent(), Uri.parse(shareBean.getHeadImg())));
                            }
                            ImageMessage imageMessage = ImageMessage.obtain(uri, uri);
                            Message myMessage = Message.obtain(shareBean.getGuid(), shareBean.getType(), imageMessage);
                            RongIM.getInstance().sendImageMessage(myMessage, null, null, new RongIMClient.SendImageMessageCallback() {
                                @Override
                                public void onAttached(Message message) {

                                }

                                @Override
                                public void onError(Message message, RongIMClient.ErrorCode errorCode) {

                                }

                                @Override
                                public void onSuccess(Message message) {

                                }

                                @Override
                                public void onProgress(Message message, int i) {

                                }
                            });
                        }
                    });
        }
    }

    public static void sendGroupMemberText(BaseActivity baseActivity, List<SelectBean> shareBeanList, String text, String groupId) {
        if (shareBeanList != null && shareBeanList.size() > 0) {
            baseActivity.showLoading();
            Observable.just(shareBeanList)
                    .subscribe(new Subscriber<List<SelectBean>>() {
                        @Override
                        public void onCompleted() {
                            baseActivity.hideLoading();
                            ToastUtil.showShort("发送成功");
                            baseActivity.finish();
                        }

                        @Override
                        public void onError(Throwable e) {

                            System.out.println(e.toString());
                        }

                        @Override
                        public void onNext(List<SelectBean> shareBeen) {
                            TextMessage myTextMessage = TextMessage.obtain(text);
                            RongIM.getInstance().sendDirectionalMessage(Conversation.ConversationType.GROUP, groupId, myTextMessage, getUserid(shareBeen),
                                    null, null, new IRongCallback.ISendMessageCallback() {
                                        @Override
                                        public void onAttached(Message message) {
                                            //消息本地数据库存储成功的回调
                                        }

                                        @Override
                                        public void onSuccess(Message message) {
                                            //消息通过网络发送成功的回调
                                            System.out.println("f发送成功");
                                        }

                                        @Override
                                        public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                            //消息发送失败的回调
                                            System.out.println(errorCode);
                                        }
                                    });
                        }
                    });
        }
    }

    private static String[] getUserid(List<SelectBean> shareBeanList) {
        String[] strings = new String[shareBeanList.size()];
        for (int i = 0; i < strings.length; i++) {
            strings[i] = shareBeanList.get(i).getGuid();
        }
        return strings;
    }
}
